package com.smedi.ismedi.personnel.core.port.adapter.restful;


import com.smedi.ismedi.personnel.api.dto.response.ProfileImageResponse;
import com.smedi.ismedi.personnel.api.dto.response.SignatureResponse;
import com.smedi.ismedi.personnel.api.dto.response.UploadPersonTemplateValidateResponse;
import com.smedi.ismedi.personnel.api.feign.RemotePersonAssistFeignApi;
import com.smedi.ismedi.personnel.core.application.service.*;
import com.smedi.ismedi.personnel.core.domain.aggregatemodel.UploadPersonTemplateValidate;
import com.smedi.ismedi.personnel.core.port.adapter.service.ImisRemoteApiService;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: xulinjun
 * @Date: 2021-6-8 13:31
 */
@Slf4j
@RestController
@AllArgsConstructor
public class PersonAssistController implements RemotePersonAssistFeignApi {
    private final ProfileImageApplicationService profileImageApplicationService;

    private final SignatureApplicationService signatureApplicationService;

    private final UploadDataApplicationService uploadDataApplicationService;

    private final ImisSyncApplicationService imisSyncApplicationService;

    private final ImisRemoteApiService imisRemoteApiService;

    private final ScheduleApplicationService scheduleApplicationService;

    private final DozerUtil dozerUtil;

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<String> postUploadProfileImage(MultipartFile file) {
        String uuid = profileImageApplicationService.uploadProfileImage(file);
        return new ResponseData<String>().success(uuid);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> postUploadProfileImageWithNoConfirm(MultipartFile file, Long personId) {
        profileImageApplicationService.uploadProfileImageAndSave(file, personId);
        return new ResponseData<String>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<ProfileImageResponse> getDownloadProfileImage(String imageUUID) {
        ProfileImageResponse response = dozerUtil.convertor(profileImageApplicationService.downloadProfileImage(imageUUID), ProfileImageResponse.class);
        return new ResponseData<ProfileImageResponse>().success(response);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<String> postUploadSignature(MultipartFile file) {
        String uuid = signatureApplicationService.uploadSignature(file);
        return new ResponseData<String>().success(uuid);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> postUploadSignatureWithNoConfirm(MultipartFile file, Long personId) {
        signatureApplicationService.uploadSignatureAndSave(file, personId);
        return new ResponseData<String>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<SignatureResponse> getDownloadSignature(String signatureUUID) {
        SignatureResponse response = dozerUtil.convertor(signatureApplicationService.downloadSignature(signatureUUID), SignatureResponse.class);
        return new ResponseData<SignatureResponse>().success(response);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<SignatureResponse> getDownloadSignatureByPersonId(Long personId) {
        SignatureResponse response = dozerUtil.convertor(signatureApplicationService.downloadSignatureByPersonId(personId), SignatureResponse.class);
        return new ResponseData<SignatureResponse>().success(response);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<UploadPersonTemplateValidateResponse> postUploadPerson(MultipartFile file, Long organizationId, Long projectId, boolean internal) throws IOException {
        UploadPersonTemplateValidate uploadPersonTemplateValidate = uploadDataApplicationService.doUpload(file, organizationId, projectId, internal);
        UploadPersonTemplateValidateResponse uploadPersonTemplateValidateResponse = dozerUtil.convertor(uploadPersonTemplateValidate, UploadPersonTemplateValidateResponse.class);
        return new ResponseData<UploadPersonTemplateValidateResponse>().success(uploadPersonTemplateValidateResponse);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<UploadPersonTemplateValidateResponse> postConfirmValidateAndSubmit(String validateId) {
        UploadPersonTemplateValidate uploadPersonTemplateValidate = uploadDataApplicationService.doConfirmValidateAndSubmit(validateId);
        UploadPersonTemplateValidateResponse uploadPersonTemplateValidateResponse = dozerUtil.convertor(uploadPersonTemplateValidate, UploadPersonTemplateValidateResponse.class);
        return new ResponseData<UploadPersonTemplateValidateResponse>().success(uploadPersonTemplateValidateResponse);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<String> getCopyProfileImageBase64FromImis(Long referencePersonId) {
        String token = imisRemoteApiService.callToken();
        String base64 = imisRemoteApiService.callImageBase64(token, referencePersonId);
        return new ResponseData<String>().success(base64);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> getSyncFromImis() {
        imisSyncApplicationService.doSync();
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> getStartScheduleSync() {
        scheduleApplicationService.runSync();
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> getEndScheduleSync() {
        scheduleApplicationService.stopSync();
        return new ResponseData<Boolean>().success(true);
    }
}
